class SubscriptionsController < ApplicationController
  def index
    @subscriptions = Subscription.find(:all)
  end

  def create
    @feed = Feed.find_by_url(params[:feed][:url]) || Feed.create(params[:feed])
    begin
      @feed.refresh
      category_id = params[:subscription][:category_id] if params[:subscription]
      @subscription = Subscription.subscribe(user,@feed,
                                            category_id) unless user.has?(@feed)
    rescue
      flash[:error]='#{params[:feed][:url]} can not be retrieved'
      render :action => 'invalid_feed'
    end

  end

  def destroy
    @subscription = Subscription.find(params[:id])
    @title = @subscription.feed.title
    @subscription.destroy
  end

  def show
    get_subscription
  end

  def mark
    get_subscription
    @subscription.unreadeds.clear
  end

  def import
    OPML.parse(params[:file].read,user)
    responds_to_parent do
      render :action => 'import'
    end
  end

  private

  def get_subscription
    @subscription = Subscription.find(params[:id])
  end


end
